package cn.yunhe.controller;


import cn.yunhe.api.IMerchantService;
import cn.yunhe.common.TestException;
import cn.yunhe.covert.TotalMsgCovert;
import cn.yunhe.dto.DateAndTotal;
import cn.yunhe.dto.TotalMsgDto;
import cn.yunhe.vo.TotalMsgVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "商户操作")
public class MerchantController {


    @Autowired
    IMerchantService merchantService;


    @PostMapping("/queryTotal")
    @ApiOperation(value = "查询某商户今日和昨日的（营业额、订单总数、人均销售额）")
    @ApiImplicitParam(name = "merchantId",value = "商户ID")
    public Map<String, TotalMsgVo> queryTotal(Long merchantId) throws TestException {
        //非空校验
        if (merchantId == null){
            throw new TestException("2000","参数为空");
        }
        //根据商户id
        Map<String, TotalMsgVo> map = new HashMap<>();
        //获取今天的订单信息
        TotalMsgDto totalMsgDto = merchantService.queryTodayTotalByMerchantId(merchantId);
        TotalMsgVo totalMsgVoToday = TotalMsgCovert.INSTANCE.dto2vo(totalMsgDto);
        map.put("today",totalMsgVoToday);

        //获取昨天的订单信息
        TotalMsgDto totalMsgDto2 = merchantService.queryYesterdayTotalByMerchantId(merchantId);
        TotalMsgVo totalMsgVoYesterday = TotalMsgCovert.INSTANCE.dto2vo(totalMsgDto2);
        map.put("yesterday",totalMsgVoYesterday);

        return map;
    }

    @PostMapping("/queryDoubleWeek")
    @ApiOperation(value = "查询某商户近两周（订单总金额、并按照日期顺序排列）")
    @ApiImplicitParam(name = "merchantId",value = "商户ID")
    public List<DateAndTotal> queryDoubleWeek(Long merchantId) throws TestException {

        //非空校验
        if (merchantId == null){
            throw new TestException("2000","参数为空");
        }

        List<DateAndTotal> list = merchantService.queryDoubleWeek(merchantId);

        return list;
    }

}